package com.nosweattechnet.placemap;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.nosweattechnet.datatypes.Item;
import com.nosweattechnet.mapmyway.R;
import com.nosweattechnet.mapmyway.ReportPlace;
import com.nosweattechnet.server.AddItemListAsyncTask;

@SuppressLint("ViewConstructor")
//Markers on the place map
public class ItemMarker extends ImageView {
	//Item it represents
	private Item item;
	
	private String userid;
	//Image used to show it
	private int draw=R.drawable.google_pin_small;
	private final PlaceMapActivity mContext;
	
	//Crates Marker and sets the functionality when clicked
	public ItemMarker(PlaceMapActivity context,Item i,String user_id) {
		super(context);
		userid=user_id;
		mContext=context;
		//Sets its drawable image
		this.setImageDrawable(getResources().getDrawable(draw));
		item=i;
		this.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				onClickDialog();
			}
		});
	}
	
	//Function called when the marker is clicked
	private void onClickDialog(){
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getName());
		dialog.setMessage(item.getDescription());
		dialog.setPositiveButton("Add to Favorites",  new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface arg0, int arg1) {
				AddItemListAsyncTask ailat= new AddItemListAsyncTask(item.getId(),userid);
				ailat.execute();
			}
		});
		dialog.setNeutralButton("Suggest New Location",  new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface arg0, int arg1) {
				mContext.openAddItemLocationActivity(item.getId());
			}
		});
		dialog.setNegativeButton("Report", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				Intent intent = new Intent(mContext, ReportPlace.class);
				intent.putExtra(mContext.getString(R.string.item_id_intent),item.getId());
				intent.putExtra(mContext.getString(R.string.name_of_item),item.getName());
				intent.putExtra(mContext.getString(R.string.user_id_intent),userid);
				mContext.startActivity(intent);
			}
		});
		dialog.show();
	}
	
	//The offset from the top left corner to position which points to actual location in the x direction
	public float XOffSet(){
		return getResources().getDrawable(draw).getMinimumWidth()/2;
	}
	
	//The offset from the top left corner to position which points to actual location in the y direction
	public float YOffSet(){
		return getResources().getDrawable(draw).getMinimumHeight();
	}

}
